package com.qixiucoding;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;

/**
 * @Description TODO
 * @Author longhj
 * @Date 2022/08/13 星期六 0:21
 */
@SpringBootTest
public class test22 {
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    long num = 1000000000;

    @Test
    public void a() throws ExecutionException, InterruptedException {
        testReg2();
        testReg();
    }

    public void calculate1w() {
        long start = System.currentTimeMillis();
        long sum = 0;
        for (int i = 0; i < num; i++) {
            sum += i;
        }
        for (int i = 0; i < num; i++) {
            sum += i;
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    public void calculate() throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();
        CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() -> {
            long sum = 0;
            for (int i = 0; i < num; i++) {
                sum += i;
            }
        }, threadPoolTaskExecutor);
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            long sum = 0;
            for (int i = 0; i < num; i++) {
                sum += i;
            }
        }, threadPoolTaskExecutor);
        CompletableFuture.allOf(voidCompletableFuture1, voidCompletableFuture).get();
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    @Test
    public void testReg2() {
        String string = FileUtil.readString("D:\\workHome\\Jack_Idea\\learnspringboot\\learn-hutool\\src\\main\\resources\\test.txt", "utf-8");
        // List<String> list = new CopyOnWriteArrayList();

        long start2 = System.currentTimeMillis();
        ArrayList<CompletableFuture<Void>> arrayList = new ArrayList<>();
        for (int i = 0; i < 100000; i++) {
            CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() -> {
                ReUtil.findAll(">\\d+(?:</span>)?、(.*?)</p>", string, 1);
            }, threadPoolTaskExecutor);
            arrayList.add(voidCompletableFuture1);
        }
        CompletableFuture.allOf(arrayList.toArray(new CompletableFuture[arrayList.size()]));
        long end2 = System.currentTimeMillis();
        System.out.println(end2 - start2);
        System.out.println("------------------");




    }
    public void testReg() {
        String string = FileUtil.readString("D:\\workHome\\Jack_Idea\\learnspringboot\\learn-hutool\\src\\main\\resources\\test.txt", "utf-8");
        // List<String> list = new CopyOnWriteArrayList();

        long start = System.currentTimeMillis();

        for (int i = 0; i < 100000; i++) {
            ReUtil.findAll(">\\d+(?:</span>)?、(.*?)</p>", string, 1);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);



    }
}
